﻿using System;
using System.Data;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FashionShop.Models;

namespace FashionShop.Areas.Home.Controllers
{
    public class CustomerController : HomeAppController
    {
        //
        // GET: /Home/Customer/

        public ActionResult Index()
        {
            List<int> temp = (List<int>)Session["ProductList"];
            
            if (temp != null)
            {
                int[] product_id_list = temp.ToArray();
                var product = db.Product.Include(p => p.Brand).Include(p => p.ProductCatalogue);
                var list = product.Where(m => product_id_list.Contains(m.ProductID)).ToList();
                ViewBag.Products = list;
          
                int price = 0;
                foreach (var s in list)
                {
                    price += s.SalePrice;
                }
                ViewBag.TotalPrice = price;
            }
            return View();
        }

        [HttpPost]
        public ActionResult Index(Customer customer)
        {
            List<int> temp = (List<int>)Session["ProductList"];
            if (ModelState.IsValid && temp != null)
            {

                db.Customer.Add(customer);
                db.SaveChanges();
                foreach (var i in temp)
                {
                    CustomerProduct cp = new CustomerProduct();
                    cp.CustomerID = customer.CustomerID;
                    cp.ProductID = i;
                    cp.AddCreatedTime();
                    db.CustomerProduct.Add(cp);
                    db.SaveChanges();
                }
                return RedirectToAction("OrderSuccess");
            }
           

            if (temp != null)
            {
                int[] product_id_list = temp.ToArray();
                var product = db.Product.Include(p => p.Brand).Include(p => p.ProductCatalogue);
                var list = product.Where(m => product_id_list.Contains(m.ProductID)).ToList();
                ViewBag.Products = list;
                int price = 0;
                foreach (var s in list)
                {
                    price += s.SalePrice;
                }
                ViewBag.TotalPrice = price;
            }
            return View(customer);
        }

        public ActionResult AddToCart(int id)
        {
            List<int> temp = (List<int>) Session["ProductList"];
            if (temp == null)
            {
                temp = new List<int>();
                Session["ProductList"] = temp;
                temp.Add(id);
            }
            else
            {
                if(temp.IndexOf(id) == -1)
                    temp.Add(id);
            }
            return RedirectToAction("Index");
        }

        public ActionResult RemoveFromCart(int id)
        {
            List<int> temp = (List<int>)Session["ProductList"];
            if (temp != null)
            {
                var index = temp.IndexOf(id);
                if (index != -1)
                    temp.RemoveAt(index);
            }

            return RedirectToAction("Index");
        }

        public ActionResult OrderSuccess()
        {
            Session.Remove("ProductList");
            return View();
        }
    }
}
